<?php
$r = "";
require_once($r."_config/db.php");
require_once($r."_engine/functions.php");
require_once($r."_engine/c_sql_table.php");
require_once($r."_engine/c_resources_storage.php");


/*$res = execute_query('
SELECT
	COUNT(*) AS cnt
FROM monitors
');*/
$res = execute_query('
SELECT
	COUNT(*) AS cnt
FROM
    download_stats
WHERE
    resource_status = '.RESOURCE_STORAGE_STATUS_CHANGED.' OR
    resource_status = '.RESOURCE_STORAGE_STATUS_TRIGGERED.'
');

$row = @mysql_fetch_array($res);
$monitors_count = $row["cnt"];


$res = execute_query('
SELECT
	MAX(resources_storage.id) AS id,
	MAX(resources_storage.download_dt) AS dt,
	resources_storage.effective_url AS url,
    resource_status
FROM resources_storage
LEFT JOIN monitors ON 
	resources_storage.monitor_id = monitors.id
WHERE
	monitors.disable_top_list_stats <> 1 AND
	resources_storage.resource_status <> '.RESOURCE_STORAGE_STATUS_FAKE.'
GROUP BY
	resources_storage.effective_url
ORDER BY
	id DESC,
	dt DESC
LIMIT 0, 5;
');

$list = array();
while ($row = @mysql_fetch_array($res))
{
    $is_changed =
        ($row["resource_status"] == RESOURCE_STORAGE_STATUS_CHANGED ||
         $row["resource_status"] == RESOURCE_STORAGE_STATUS_TRIGGERED);
        
    $list[] = array(
        'u'     => normalize_url($row["url"]),
        'd'     => domain_name_from_url($row["url"]),
        'ch'    => $is_changed,
    );
}


/* ------------------------------------------------- */
/* -----               HTML Output             ----- */
/* ------------------------------------------------- */
?>

<p class="text"><span class="hdrBlack">
Tracking <a href="<?=$r?>monitored_sites.php"><?=$monitors_count?></a> updates and counting!</span></p>

<p class="textSmall">
<?php
foreach ($list as $data)
{
    if ($data['ch'])
        $s = '<span class="w3color">updated</span>';
    else
        $s = 'not&nbsp;changed';
    
    echo '<a href="'.$data['u'].'">'.$data['d'].'</a>&nbsp;&nbsp;[&nbsp;'.$s.'&nbsp;] <br>';
}
?>
</p>

